#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExp>
#include <QLineEdit>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    ui->lineEdit->setText("张三");
    ui->lineEdit->setReadOnly(true);

    ui->lineEdit_2->setClearButtonEnabled(true);
    ui->lineEdit_2->setPlaceholderText("输入密码");
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);

//    ui->lineEdit_3->setInputMask("000-0000-0000");
    //使用正则表达式表示电话号码
    // 给 lineEdit 注册一个验证器 - validator
    ui->lineEdit_3->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));
    ui->pushButton->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_lineEdit_3_textEdited(const QString &text)
{
    qDebug() << text;
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_3->validator()->validate(content,pos) == QValidator::Acceptable)
    {
        //验证通过，按钮设置可用状态
        ui->pushButton->setEnabled(true);
    }
    else
    {
        //验证不通过，按钮设置禁用状态
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    qDebug() << "on_checkBox_toggled " << checked;
    if(checked == true)
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);
    else
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "on_pushButton_clicked";
    QString gender = ui->radioButton->isChecked() == true ? "男" : "女";
    qDebug() << ui->label->text() << ui->lineEdit->text()
             << "," << ui->label_2->text() << ui->lineEdit_2->text()
             << "," << ui->label_3->text() << gender
             << "," << ui->label_4->text() << ui->lineEdit_3->text();
}
